#include "main.h"
#include "func_system.h"
#include "func_led.h"
#include "cmsis_os.h"
#include "FreeRTOS.h"
#include "task.h"

void led_TaskFunction(void *pvParameters)
{
  LEDBLINK_TYPE led_y;
  LEDBLINK_TYPE led_b;
  LEDTaskParaTypeDef* para = pvParameters;
  led_y.gpio = para->yellow_gpio;
  led_y.pin = para->yellow_pin;
  led_y.mode = LED_OFF;
  led_y.tick = 0;
  led_b.gpio = para->blue_gpio;
  led_b.pin = para->blue_pin;
  led_b.mode = LED_BLINK;
  led_b.tick = 0;
  HAL_GPIO_WritePin(para->blue_gpio,para->blue_pin,GPIO_PIN_RESET);
  HAL_GPIO_WritePin(para->yellow_gpio,para->yellow_pin,GPIO_PIN_RESET);
  vTaskDelay(2000/portTICK_RATE_MS);
  while(1)
  {
    LEDBlink_handler(&led_y);
    LEDBlink_handler(&led_b);
    vTaskDelay(250/portTICK_RATE_MS);
  }
  vTaskDelete(NULL);
}

void LEDBlink_handler(LEDBLINK_TYPE* led)
{
  switch(led->mode)
  {
  case LED_OFF:
    led->tick = 0;
    HAL_GPIO_WritePin(led->gpio,led->pin,GPIO_PIN_SET);
    break;
  case LED_ON:
    led->tick = 0;
    HAL_GPIO_WritePin(led->gpio,led->pin,GPIO_PIN_RESET);
    break;
  case LED_SLOW:
    if(led->tick == 0)
      HAL_GPIO_TogglePin(led->gpio,led->pin);
    led->tick++;
    if(led->tick>5)
      led->tick = 0;
    break;
  case LED_FAST:
    led->tick = 0;
    HAL_GPIO_TogglePin(led->gpio,led->pin);
    case LED_BLINK:
    if(led->tick == 0)
      HAL_GPIO_WritePin(led->gpio,led->pin,GPIO_PIN_RESET);
    else if(led->tick == 2)
      HAL_GPIO_WritePin(led->gpio,led->pin,GPIO_PIN_SET);
    led->tick++;
    if(led->tick>25)
      led->tick = 0;
    break;
  default:
    led->mode = LED_OFF;
  }
}